package src.ee.ioc.smaug.parser;

import java.io.*;

public class XMLWriter {
    private OutputStream fileXMLRes = null;

    private OutputStream buffXMLRes = null;

    private OutputStreamWriter writerXMLRes = null;
    
    private int shift=0;

    
    private void writeShift() {
        try {
            for (int i=0; i<shift;i++) {
                writerXMLRes.write("   ");
            }
            
        }

        catch (IOException e) {
            System.err.println(e.getMessage());
        }
    }
    /**
     * @param writer
     */
    public XMLWriter(String fileName) {
        try {
            fileXMLRes = new FileOutputStream(fileName);
            buffXMLRes = new BufferedOutputStream(fileXMLRes);
            writerXMLRes = new OutputStreamWriter(buffXMLRes, "UTF8");

        } catch (UnsupportedEncodingException e) {
            System.err.println("Something wrong with ecoding.");
        } catch (IOException e) {
            System.err.println(e.getMessage());
        }

    }

    public void openEntryTag(String entry) {
        try {
            writerXMLRes.write("\r\n");
            writeShift();
            writerXMLRes.write("<" + entry);
            shift++;
        }

        catch (IOException e) {
            System.err.println(e.getMessage());
        }
    }

    public void closeEntryTag() {
        try {
            writerXMLRes.write(">");
        }

        catch (IOException e) {
            System.err.println(e.getMessage());
        }
    }

    public void closeEntry(String entry) {
        try {
            shift--;
            writeShift();
            writerXMLRes.write("</" + entry + ">\r\n");
            
        }

        catch (IOException e) {
            System.err.println(e.getMessage());
        }
    }

    public void writeAttribute(String attr, String value) {
        try {
            writerXMLRes.write(" " + attr + "=\"" + value + "\"");
        } catch (IOException e) {
            System.err.println(e.getMessage());
        }

    }

    public void closeWriter() {
        try {
            writerXMLRes.flush();
            writerXMLRes.close();
        }

        catch (IOException e) {
            System.err.println(e.getMessage());
        }

    }

    public void writePreface(String ver, String enc, String dtd) {
        try {
            writerXMLRes.write("<?xml version=\"" + ver + "\" ");
            writerXMLRes.write("encoding=\"" + enc
                    + "\" standalone=\"yes\"?>\r\n");
            writerXMLRes.write("<!DOCTYPE fsm SYSTEM \""+dtd+"\">\r\n");
        } catch (IOException e) {
            System.err.println(e.getMessage());
        }
    }

    public void writeStylesheet(String sheet) {
        try {
            writerXMLRes.write("<?xml-stylesheet type=\"text/xsl\" href=\""
                    + sheet + "\"?>\r\n");
        } catch (IOException e) {
            System.err.println(e.getMessage());
        }
    }

}
